package com.zhongwang.mms.module.half.service;

import com.zhongwang.mms.gen.entity.*;
import com.zhongwang.mms.module.allocation.dao.AllocationOrderDao;
import com.zhongwang.mms.module.half.dao.HInventoryDao;
import com.zhongwang.mms.module.half.dao.HOrderDao;
import com.zhongwang.mms.module.half.dao.HOrderDetailDao;
import com.zhongwang.mms.module.half.dao.PAllocationRecordDao;
import com.zhongwang.mms.module.half.model.HOrderDetailModel;
import com.zhongwang.mms.module.system.log.LogService;
import com.zhongwang.mms.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @description:红冲
 * @author: th
 * @time: 2020/4/17 8:10
 */
@Service
public class RedOrderDetailService {

    @Autowired
    private HOrderDetailDao hOrderDetailDao;
    @Autowired
    private HOrderDao hOrderDao;
    @Autowired
    private PAllocationRecordDao pAllocationRecordDao;
    @Autowired
    private HInventoryDao hInventoryDao;
    @Autowired
    private HOrderDetailService hOrderDetailService;
    @Autowired
    private LogService logService;
    @Autowired
    private AllocationOrderDao allocationOrderDao;

    /**
     * 查询单据红冲页面数据
     */
    public List<HOrderDetailModel> selectAllByQuery(HOrderDetailModel query) {

        return hOrderDetailDao.selectAllByQuery(query);
    }

    /**
     * 单条红冲
     */
    @Transactional
    public int updateDetailToRedByOdaId(Integer odaId) {
        User user = ShiroUtils.getUser();
        List<HInventory> hInventorys;
        HInventory hInventory;
        //根据子单ID查询子单和主单信息
        HOrderDetailModel hOrderDetailModel = hOrderDetailDao.selectAllByOdaId(odaId);
        HOrder hOrder = hOrderDao.selectOneByPrimaryKey(hOrderDetailModel.getOdaOdrId());
        if (hOrderDetailModel.getOdrType() == 1) {
            //根据子单ID查询库存信息
            hInventorys = hInventoryDao.selectAllByRowNo(odaId);

            //如果有两条以上的同子单库存  不能冲单
            if (hInventorys.size() > 1) {
                return 0;
            } else if(hInventorys.size() == 1){
                hInventory = hInventorys.get(0);
                if (!hInventory.getItyAvaNum().equals(hInventory.getItyNum())) {
                    return 0;
                }
                if (!hInventory.getItyNum().equals(hOrderDetailModel.getOdaNum())) {
                    return 0;
                }
            }else{
                return 0;
            }
        } else {
            hInventory = hInventoryDao.selectAllById(hOrderDetailModel.getOdaOutItyId());
        }
        Double detailNum = 0 - hOrderDetailModel.getOdaNum();
        if (hOrderDetailModel.getOdrType() == 1) {
            hInventory.setItyNum(hInventory.getItyNum() + detailNum);
            hInventory.setItyAvaNum(hInventory.getItyAvaNum() + detailNum);
            if (hOrderDetailModel.getOdaWeight() != null) {
                Double detailWeight = 0 - hOrderDetailModel.getOdaWeight();
                hInventory.setItyWeight(hInventory.getItyWeight() + detailWeight);
                hInventory.setItyAvaWeight(hInventory.getItyAvaWeight() + detailWeight);
            }
        } else {
            hInventory.setItyNum(hInventory.getItyNum() - detailNum);
            hInventory.setItyAvaNum(hInventory.getItyAvaNum() - detailNum);
            if (hOrderDetailModel.getOdaWeight() != null) {
                Double detailWeight = 0 - hOrderDetailModel.getOdaWeight();
                hInventory.setItyWeight(hInventory.getItyWeight() - detailWeight);
                hInventory.setItyAvaWeight(hInventory.getItyAvaWeight() - detailWeight);
            }
        }
        if (hInventory.getItyAvaNum() < 0 || hInventory.getItyNum() < 0) {
            return 0;
        } else {
            if (hOrder.getOdrType() == 2) {
                //修改库存数量
                if (hInventory.getItyWeight() != null) {
                    hInventoryDao.updateHInventoryNumById(hInventory);
                } else {
                    hInventoryDao.updateHInventoryNumById2(hInventory);
                }
            } else if (hOrder.getOdrType() == 1) {
                //修改库存数量
                if (hInventory.getItyWeight() != null) {
                    hInventoryDao.updateHInventoryNumByRowNo(hInventory);
                } else {
                    hInventoryDao.updateHInventoryNumByRowNo2(hInventory);
                }
            }


            //生成数量相反的子单数据
            hOrderDetailModel.setOdaRushId(odaId);
            hOrderDetailModel.setOdaNum(detailNum);
            hOrderDetailModel.setOdaAvaNum(detailNum);
            hOrderDetailModel.setOdaRedFlag(1);
            hOrderDetailModel.setOdaRedDate(new Date());
            hOrderDetailModel.setOdaRedUser(user.getAccount());
            hOrderDetailService.saveHOrderDetail(hOrderDetailModel);
            //修改子单红冲状态
            HOrderDetail hOrderDetail = new HOrderDetail();
            hOrderDetail.setOdaId(odaId);
            hOrderDetail.setOdaRedFlag(1);
            hOrderDetail.setOdaRedDate(new Date());
            hOrderDetail.setOdaRedUser(user.getName());
            logService.saveLog("【成品库单据冲单】", "单据编号：" + hOrderDetailModel.getOdrCode() + "物料描述：" + hOrderDetailModel.getOdaMatName());
            return hOrderDetailDao.updateDetailToRedByOdaId(hOrderDetail);
        }
    }

    /**
     * 批量红冲
     */
    @Transactional
    public String toRedTogether(List<HOrderDetailModel> hOrderDetailModels) {

        String msg = "";
        if (hOrderDetailModels.size() == 1 && hOrderDetailModels.get(0).getOdrOutType() == 3) {
            msg = "全部选中";
        }
        for (int i = 0; i < hOrderDetailModels.size() - 1; i++) {
            if (hOrderDetailModels.get(i).getOdrRepId() != null && hOrderDetailModels.get(i).getOdrRepId().equals(hOrderDetailModels.get(i + 1).getOdrRepId())) {
                //msg = "同一调拨单";
                //判断勾选数量和调拨数量是否相等
                int count = hOrderDetailDao.selectCountByOdrRepId(hOrderDetailModels.get(i).getOdrRepId());
                if (count == hOrderDetailModels.size()) {
                    msg = "全部选中";
                } else {
                    msg = "未选全";
                }
            } else {
                //msg = "不同";
                if (hOrderDetailModels.get(i).getOdrOutType() == 3) {
                    msg = "有调拨";
                } else {
                    msg = "正常批量红冲";
                }
            }
            if (msg.equals("未选全") || msg.equals("有调拨")) {
                break;
            }
        }
        if (msg.equals("全部选中")) {
            for (int j = 0; j < hOrderDetailModels.size(); j++) {
                //红冲子单
                int num = updateDetailToRedByOdaId(hOrderDetailModels.get(j).getOdaId());
                if (num == 0) {
                    msg = "库存数量不足";
                    break;
                } else {
                    if (hOrderDetailModels.get(j).getOdrType() == 1) {
                        //回写调拨单状态
                        //pAllocationRecordDao.updateExecStatusByArdFormCode(hOrderDetailModels.get(j).getOdrRepId());
                        AllocationOrder allocationOrder = new AllocationOrder();
                        allocationOrder.setOdrCode(hOrderDetailModels.get(j).getOdrRepId());
                        allocationOrder.setOdrOutStatus(2);
                        allocationOrderDao.updateOutStatusByOdrCode(allocationOrder);
                    }
                }
            }
        } else if (msg.equals("正常批量红冲")) {
            for (int k = 0; k < hOrderDetailModels.size(); k++) {
                int n = updateDetailToRedByOdaId(hOrderDetailModels.get(k).getOdaId());
                if (n == 0) {
                    msg = "库存数量不足";
                    break;
                }
            }
        }
        return msg;
    }
}
